﻿// ----------------------------------------------------------------
// Description : 实体类,该文件由MyGeneration生成
// Author      : 
// Create date : 2009-12-05
// ----------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace ITOMMS.Entity
{
	/// <summary>
	///	tbPurchaseApply Entity
	///	tbPurchaseApply Entity采购申请表
	/// </summary>
	[Serializable]
	public class tbPurchaseApplyEntity : BaseEntity<tbPurchaseApplyEntity,int>
	{
		
		#region 私有成员			
		
		private int _ID; 
		private int? _AssetClassID; 
		private string _AssetName; 
		private int? _AssetNum; 
		private decimal? _EstimatedPrice; 
		private DateTime? _ExpectTime; 
		private DateTime? _DeliveryTime; 
		private int? _PurchaseCompany; 
		private string _User; 
		private string _AssetDescription; 
		private int? _PurchaseState; 
		private DateTime? _CreateDate; 
		private int? _CreateBy; 
		private DateTime? _ModifyDate; 
		private int? _ModifyBy; 
		private int _SEQNO;
		#endregion


		
		#region 默认( 空 ) 构造函数
		/// <summary>
		/// 默认构造函数
		/// </summary>
		public tbPurchaseApplyEntity()
		{
			_ID = 0; 
			_AssetClassID =  null; 
			_AssetName = String.Empty; 
			_AssetNum =  null; 
			_EstimatedPrice =  null; 
			_ExpectTime =  null; 
			_DeliveryTime =  null; 
			_PurchaseCompany =  null; 
			_User = String.Empty; 
			_AssetDescription = String.Empty; 
			_PurchaseState =  null; 
			_CreateDate =  null; 
			_CreateBy =  null; 
			_ModifyDate =  null; 
			_ModifyBy =  null; 
			_SEQNO = 0;
		}
		#endregion
		
		#region 属性值校验
		
		/// <summary>
		/// 属性值校验
		/// </summary>
		public bool CheckValues()
		{
			bool bResult = true;
			string strError = "";
		
			if ( _ID ==  0)
			{
				bResult = false;
				strError += "\r\n 非空值不能为: "+_ID;
			}
			
			if( !bResult )
					throw new ArgumentOutOfRangeException(strError);
			return bResult;
			
		}
		
		#endregion
		
		
		
	
		#region Required Fields Only Constructor
		
		/// <summary>
		/// required (not null) fields only constructor
		/// </summary>
		public tbPurchaseApplyEntity(
		int para_ID		)
		: this()
		{
					_ID = para_ID;
			_AssetClassID = null;
			_AssetName = String.Empty;
			_AssetNum = null;
			_EstimatedPrice = null;
			_ExpectTime = null;
			_DeliveryTime = null;
			_PurchaseCompany = null;
			_User = String.Empty;
			_AssetDescription = String.Empty;
			_PurchaseState = null;
			_CreateDate = null;
			_CreateBy = null;
			_ModifyDate = null;
			_ModifyBy = null;
		}
		#endregion 

		
		#region 公有属性
			
			/// <summary>
			/// 行号，顺序号。系统默认的属性
			/// </summary>
			public virtual int SEQNO
			{
				get { return _SEQNO; }
				set { _SEQNO = value; }
			}
		
			
		/// <summary>
		/// IDID
		/// </summary>		
		public virtual int ID
		{
			get { return _ID; }
			set { _ID = value; }
		}
			
		/// <summary>
		/// AssetClassID资产类别ID
		/// </summary>		
		public virtual int? AssetClassID
		{
			get { return _AssetClassID; }
			set { _AssetClassID = value; }
		}
			
		/// <summary>
		/// AssetName资产名称
		/// </summary>		
		public virtual string AssetName
		{
			get { return _AssetName; }
			set	
			{
				
				if ( value != null)
					if( value.Length > 100)
						throw new ArgumentOutOfRangeException("Invalid value for AssetName", value, value.ToString());
				
				_AssetName = value;
			}
		}
			
		/// <summary>
		/// AssetNum资产数量
		/// </summary>		
		public virtual int? AssetNum
		{
			get { return _AssetNum; }
			set { _AssetNum = value; }
		}
			
		/// <summary>
		/// EstimatedPrice估计价格
		/// </summary>		
		public virtual decimal? EstimatedPrice
		{
			get { return _EstimatedPrice; }
			set { _EstimatedPrice = value; }
		}
			
		/// <summary>
		/// ExpectTime期望时间
		/// </summary>		
		public virtual DateTime? ExpectTime
		{
			get { return _ExpectTime; }
			set { _ExpectTime = value; }
		}
			
		/// <summary>
		/// DeliveryTime交付时间
		/// </summary>		
		public virtual DateTime? DeliveryTime
		{
			get { return _DeliveryTime; }
			set { _DeliveryTime = value; }
		}
			
		/// <summary>
		/// PurchaseCompany采购单位
		/// </summary>		
		public virtual int? PurchaseCompany
		{
			get { return _PurchaseCompany; }
			set { _PurchaseCompany = value; }
		}
			
		/// <summary>
		/// User使用人
		/// </summary>		
		public virtual string User
		{
			get { return _User; }
			set	
			{
				
				if ( value != null)
					if( value.Length > 50)
						throw new ArgumentOutOfRangeException("Invalid value for User", value, value.ToString());
				
				_User = value;
			}
		}
			
		/// <summary>
		/// AssetDescription资产描述
		/// </summary>		
		public virtual string AssetDescription
		{
			get { return _AssetDescription; }
			set	
			{
				
				if ( value != null)
					if( value.Length > 2000)
						throw new ArgumentOutOfRangeException("Invalid value for AssetDescription", value, value.ToString());
				
				_AssetDescription = value;
			}
		}
			
		/// <summary>
		/// PurchaseState采购状态
		/// </summary>		
		public virtual int? PurchaseState
		{
			get { return _PurchaseState; }
			set { _PurchaseState = value; }
		}
			
		/// <summary>
		/// CreateDate创建时间
		/// </summary>		
		public virtual DateTime? CreateDate
		{
			get { return _CreateDate; }
			set { _CreateDate = value; }
		}
			
		/// <summary>
		/// CreateBy创建人
		/// </summary>		
		public virtual int? CreateBy
		{
			get { return _CreateBy; }
			set { _CreateBy = value; }
		}
			
		/// <summary>
		/// ModifyDate修改时间
		/// </summary>		
		public virtual DateTime? ModifyDate
		{
			get { return _ModifyDate; }
			set { _ModifyDate = value; }
		}
			
		/// <summary>
		/// ModifyBy修改人
		/// </summary>		
		public virtual int? ModifyBy
		{
			get { return _ModifyBy; }
			set { _ModifyBy = value; }
		}
			


		#endregion 	
		
		
        #region DAO操作
		
		
		/// <summary>
        /// 获取所有实体的列表
        /// </summary>
        /// <returns></returns>
        public override List<tbPurchaseApplyEntity> GetAll()
        {
            StringBuilder strSql = new StringBuilder();
			strSql.Append("select [ID],[AssetClassID],[AssetName],[AssetNum],[EstimatedPrice],[ExpectTime],[DeliveryTime],[PurchaseCompany],[User],[AssetDescription],[PurchaseState],[CreateDate],[CreateBy],[ModifyDate],[ModifyBy] from [tb_PurchaseApply] ");
	
            List<tbPurchaseApplyEntity> list = new List<tbPurchaseApplyEntity>();
            Database db = GetDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
				int seq = 0;
                while (dataReader.Read())
                {	seq++;
                    list.Add(ReaderBind(dataReader,seq));
                }
            }
            return list;
        }
		
		/// <summary>
        /// 获取所有实体的列表总行数
        /// </summary>
        /// <returns></returns>
        public override int GetTotalRecords()
        {
            StringBuilder strSql = new StringBuilder();
			strSql.Append("  select count([ID])  from [tb_PurchaseApply] ");
	
            Database db = GetDatabase();
            return (int)db.ExecuteScalar(CommandType.Text, strSql.ToString());
        }
		
		/// <summary>
        /// 获取所有实体的列表总行数
        /// </summary>
        /// <returns></returns>
        public override int GetTotalRecords(string sqlCondition)
        {
            StringBuilder strSql = new StringBuilder();
			strSql.Append("  select count([ID])  from [tb_PurchaseApply]  ");
	
			 if (sqlCondition.Trim() != "")
            {
                strSql.Append(" where " + sqlCondition);
            }
            Database db = GetDatabase();
            return (int)db.ExecuteScalar(CommandType.Text, strSql.ToString());
        }	
		
		
		/// <summary>
        /// 按条件查询,得到一页的对应的结果列表
        /// </summary>
        /// <param name="strWhere">条件</param>
		/// <param name="PageSize">每页大小</param>
		/// <param name="CurrentPageIndex">当前页码</param>
        /// <returns></returns>
		 public override List<tbPurchaseApplyEntity> GetAllByQuery(string strWhere, int PageSize, int CurrentPageIndex)
        {
            StringBuilder strSql = new StringBuilder();
            if (strWhere.Contains("order".ToLower()))
            {
                throw new Exception("分页查询的sql条件中不能包括排序子句，请调用重载方法实现有条件的排序筛选");
            }
		 
			strSql.Append("select top ").Append(PageSize).Append("  [ID],[AssetClassID],[AssetName],[AssetNum],[EstimatedPrice],[ExpectTime],[DeliveryTime],[PurchaseCompany],[User],[AssetDescription],[PurchaseState],[CreateDate],[CreateBy],[ModifyDate],[ModifyBy] from [tb_PurchaseApply] where ");

			 if (strWhere.Trim() != "")
            {
                strSql.Append("  " + strWhere + " and ");
            }
			  strSql.Append(" ID not in ( ");
			 strSql.Append(" select top ").Append(PageSize*(CurrentPageIndex-1)).Append("  [ID] from [tb_PurchaseApply] ");

            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere );
            }
			strSql.Append(" ) " );

            List<tbPurchaseApplyEntity> list = new List<tbPurchaseApplyEntity>();
            Database db = GetDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
			   int seq = PageSize * (CurrentPageIndex - 1);
                while (dataReader.Read())
                {
					seq++;
                    list.Add(ReaderBind(dataReader, seq));
                }
            }
            return list;
        }
		
		
			/// <summary>
        /// 按条件查询,得到一页的对应的结果列表
        /// </summary>
        /// <param name="strWhere">条件</param>
		/// <param name="PageSize">每页大小</param>
		/// <param name="CurrentPageIndex">当前页码</param>
		/// <param name="strSort">ex:order by id desc</param>
        /// <returns></returns>
		 public override List<tbPurchaseApplyEntity> GetAllByQuery(string strWhere,string strSort, int PageSize, int CurrentPageIndex)
        {
            StringBuilder strSql = new StringBuilder();
            if (strWhere.Contains("order".ToLower()))
            {
                throw new Exception("分页查询的sql条件中不能包括排序子句，请调用重载方法实现有条件的排序筛选");
            }
		 
			strSql.Append("select top ").Append(PageSize).Append("  [ID],[AssetClassID],[AssetName],[AssetNum],[EstimatedPrice],[ExpectTime],[DeliveryTime],[PurchaseCompany],[User],[AssetDescription],[PurchaseState],[CreateDate],[CreateBy],[ModifyDate],[ModifyBy] from [tb_PurchaseApply] where ");

			 if (strWhere.Trim() != "")
            {
                strSql.Append("  " + strWhere + " and ");
            }
			  strSql.Append(" ID not in ( ");
			 strSql.Append(" select top ").Append(PageSize*(CurrentPageIndex-1)).Append("  [ID] from [tb_PurchaseApply] ");

            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere+" " ).Append(strSort);
            }
			strSql.Append(" ) " ).Append(strSort);

            List<tbPurchaseApplyEntity> list = new List<tbPurchaseApplyEntity>();
            Database db = GetDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
				int seq = PageSize * (CurrentPageIndex - 1);
                while (dataReader.Read())
                {	
					seq++;
                    list.Add(ReaderBind(dataReader, seq));
                }
            }
            return list;
        }
		
		
		/// <summary>
        /// 按条件查询,得到对应的结果列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public override List<tbPurchaseApplyEntity> GetAllByQuery(string strWhere)
        {
			strWhere = SQLInjectionFilter(strWhere);
            StringBuilder strSql = new StringBuilder();
			strSql.Append("select [ID],[AssetClassID],[AssetName],[AssetNum],[EstimatedPrice],[ExpectTime],[DeliveryTime],[PurchaseCompany],[User],[AssetDescription],[PurchaseState],[CreateDate],[CreateBy],[ModifyDate],[ModifyBy] from tb_PurchaseApply ");

            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<tbPurchaseApplyEntity> list = new List<tbPurchaseApplyEntity>();
            Database db = GetDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            { 
				int seq = 0;
                while (dataReader.Read())
                {
					seq++;
                    list.Add(ReaderBind(dataReader,seq));
                }
            }
            return list;
        }
		
		
		
		/// <summary>
        /// 按条件查询,得到对应的结果列表
        /// </summary>
        /// <param name="strWhere"></param>
		/// <param name="parameters">sql条件中的参数</param>
        /// <returns></returns>
        public override List<tbPurchaseApplyEntity> GetAllByQuery(string strWhere, List<IDataParameter> parameters)
        {
            StringBuilder strSql = new StringBuilder();
			strSql.Append("select [ID],[AssetClassID],[AssetName],[AssetNum],[EstimatedPrice],[ExpectTime],[DeliveryTime],[PurchaseCompany],[User],[AssetDescription],[PurchaseState],[CreateDate],[CreateBy],[ModifyDate],[ModifyBy] from [tb_PurchaseApply] ");

            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<tbPurchaseApplyEntity> list = new List<tbPurchaseApplyEntity>();
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            foreach (IDataParameter sp in parameters)
            {
                db.AddInParameter(dbCommand, sp.ParameterName, sp.DbType, sp.Value);
            }

            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
				int seq =0 ;
                while (dataReader.Read())
                {
					seq++;
                    list.Add(ReaderBind(dataReader,seq));
                }
            }
            return list;
        }
		
		
		

		
		
		/// <summary>
        /// 获取按条件查询以后的结果列表的前指定rowNumber行记录
        /// </summary>
        /// <param name="strWhere">不带where关键字的SQL条件语句</param>
        /// <param name="rowNumber"></param>
        /// <returns></returns>
        public override List<tbPurchaseApplyEntity> GetAllByQuery(string strWhere, List<IDataParameter> parameters, int rowNumber)
        {

            StringBuilder strSql = new StringBuilder();
			strSql.Append("select top ").Append(rowNumber).Append("  [ID],[AssetClassID],[AssetName],[AssetNum],[EstimatedPrice],[ExpectTime],[DeliveryTime],[PurchaseCompany],[User],[AssetDescription],[PurchaseState],[CreateDate],[CreateBy],[ModifyDate],[ModifyBy] from [tb_PurchaseApply] ");

            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<tbPurchaseApplyEntity> list = new List<tbPurchaseApplyEntity>();
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            foreach (IDataParameter sp in parameters)
            {
                db.AddInParameter(dbCommand, sp.ParameterName, sp.DbType, sp.Value);
            }

            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {	
				int seq = 0;
                while (dataReader.Read())
                {
					seq++;
                    list.Add(ReaderBind(dataReader,seq));
                }
            }
            return list;
        }
		
		
		/// <summary>
        /// 获取按条件查询以后的结果列表的前指定rowNumber行记录
        /// </summary>
        /// <param name="strWhere">不带where关键字的SQL条件语句: AND AA=@AA</param>
        /// <param name="rowNumber"></param>
        /// <returns></returns>
        public override List<tbPurchaseApplyEntity> GetAllByQuery(string strWhere, int rowNumber)
        {
		    strWhere = SQLInjectionFilter(strWhere);
            StringBuilder strSql = new StringBuilder();
			
			strSql.Append("select top ").Append(rowNumber).Append("  [ID],[AssetClassID],[AssetName],[AssetNum],[EstimatedPrice],[ExpectTime],[DeliveryTime],[PurchaseCompany],[User],[AssetDescription],[PurchaseState],[CreateDate],[CreateBy],[ModifyDate],[ModifyBy] from [tb_PurchaseApply] ");

            if (strWhere.Trim() != "")
            {
                strSql.Append(" where  " + strWhere);
            }
            List<tbPurchaseApplyEntity> list = new List<tbPurchaseApplyEntity>();
            Database db = GetDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
				int seq=0;
                while (dataReader.Read())
                {	
					seq++;
                    list.Add(ReaderBind(dataReader,seq));
                }
            }
            return list;
        }
		
		
		
		
		
		
		
        /// <summary>
        /// 按主键查询对应的实体
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public override tbPurchaseApplyEntity GetById(int id)
        {
            StringBuilder strSql = new StringBuilder();
			strSql.Append("select [ID],[AssetClassID],[AssetName],[AssetNum],[EstimatedPrice],[ExpectTime],[DeliveryTime],[PurchaseCompany],[User],[AssetDescription],[PurchaseState],[CreateDate],[CreateBy],[ModifyDate],[ModifyBy] from [tb_PurchaseApply]  where ID= @ID ");
				
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, id);
            tbPurchaseApplyEntity entity = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    entity = ReaderBind(dataReader);
                }
            }
            return entity;

        }
		
		
		 /// <summary>
        /// 保存实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public override int Save()
        {
			
            if (this.ID ==0)
            {
                this.ID = GetMax("tb_PurchaseApply");
            }
            StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into  tb_PurchaseApply (");
			strSql.Append("[ID],[AssetClassID],[AssetName],[AssetNum],[EstimatedPrice],[ExpectTime],[DeliveryTime],[PurchaseCompany],[User],[AssetDescription],[PurchaseState],[CreateDate],[CreateBy],[ModifyDate],[ModifyBy] )");
			strSql.Append(" values (");
			strSql.Append("@ID,@AssetClassID,@AssetName,@AssetNum,@EstimatedPrice,@ExpectTime,@DeliveryTime,@PurchaseCompany,@User,@AssetDescription,@PurchaseState,@CreateDate,@CreateBy,@ModifyDate,@ModifyBy)");

            Database db = GetDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
			db.AddInParameter(dbCommand, "ID", DbType.Int32, this.ID);
			if (this.AssetClassID == null)
			{
			db.AddInParameter(dbCommand, "AssetClassID", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "AssetClassID", DbType.Int32, this.AssetClassID.Value);
			}
			db.AddInParameter(dbCommand, "AssetName", DbType.String, this.AssetName);
			if (this.AssetNum == null)
			{
			db.AddInParameter(dbCommand, "AssetNum", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "AssetNum", DbType.Int32, this.AssetNum.Value);
			}
			if (this.EstimatedPrice == null)
			{
			db.AddInParameter(dbCommand, "EstimatedPrice", DbType.Decimal, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "EstimatedPrice", DbType.Decimal, this.EstimatedPrice.Value);
			}
			if (this.ExpectTime == null)
			{
			db.AddInParameter(dbCommand, "ExpectTime", DbType.DateTime, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "ExpectTime", DbType.DateTime, this.ExpectTime.Value);
			}
			if (this.DeliveryTime == null)
			{
			db.AddInParameter(dbCommand, "DeliveryTime", DbType.DateTime, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "DeliveryTime", DbType.DateTime, this.DeliveryTime.Value);
			}
			if (this.PurchaseCompany == null)
			{
			db.AddInParameter(dbCommand, "PurchaseCompany", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "PurchaseCompany", DbType.Int32, this.PurchaseCompany.Value);
			}
			db.AddInParameter(dbCommand, "User", DbType.String, this.User);
			db.AddInParameter(dbCommand, "AssetDescription", DbType.String, this.AssetDescription);
			if (this.PurchaseState == null)
			{
			db.AddInParameter(dbCommand, "PurchaseState", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "PurchaseState", DbType.Int32, this.PurchaseState.Value);
			}
			if (this.CreateDate == null)
			{
			db.AddInParameter(dbCommand, "CreateDate", DbType.DateTime, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "CreateDate", DbType.DateTime, this.CreateDate.Value);
			}
			if (this.CreateBy == null)
			{
			db.AddInParameter(dbCommand, "CreateBy", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "CreateBy", DbType.Int32, this.CreateBy.Value);
			}
			if (this.ModifyDate == null)
			{
			db.AddInParameter(dbCommand, "ModifyDate", DbType.DateTime, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "ModifyDate", DbType.DateTime, this.ModifyDate.Value);
			}
			if (this.ModifyBy == null)
			{
			db.AddInParameter(dbCommand, "ModifyBy", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "ModifyBy", DbType.Int32, this.ModifyBy.Value);
			}

			return db.ExecuteNonQuery(dbCommand);
		  
      
        }
		

		
        /// <summary>
        /// 保存实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public override int Save(tbPurchaseApplyEntity entity)
        {
			
            if (entity.ID == 0)
            {
                entity.ID = GetMax("tb_PurchaseApply");
            }
            StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into  tb_PurchaseApply (");
			strSql.Append("[ID],[AssetClassID],[AssetName],[AssetNum],[EstimatedPrice],[ExpectTime],[DeliveryTime],[PurchaseCompany],[User],[AssetDescription],[PurchaseState],[CreateDate],[CreateBy],[ModifyDate],[ModifyBy] )");
			strSql.Append(" values (");
			strSql.Append("@ID,@AssetClassID,@AssetName,@AssetNum,@EstimatedPrice,@ExpectTime,@DeliveryTime,@PurchaseCompany,@User,@AssetDescription,@PurchaseState,@CreateDate,@CreateBy,@ModifyDate,@ModifyBy)");

            Database db = GetDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
			db.AddInParameter(dbCommand, "ID", DbType.Int32, entity.ID);
			if (entity.AssetClassID == null)
			{
			db.AddInParameter(dbCommand, "AssetClassID", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "AssetClassID", DbType.Int32, entity.AssetClassID.Value);
			}
			db.AddInParameter(dbCommand, "AssetName", DbType.String, entity.AssetName);
			if (entity.AssetNum == null)
			{
			db.AddInParameter(dbCommand, "AssetNum", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "AssetNum", DbType.Int32, entity.AssetNum.Value);
			}
			if (entity.EstimatedPrice == null)
			{
			db.AddInParameter(dbCommand, "EstimatedPrice", DbType.Decimal, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "EstimatedPrice", DbType.Decimal, entity.EstimatedPrice.Value);
			}
			if (entity.ExpectTime == null)
			{
			db.AddInParameter(dbCommand, "ExpectTime", DbType.DateTime, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "ExpectTime", DbType.DateTime, entity.ExpectTime.Value);
			}
			if (entity.DeliveryTime == null)
			{
			db.AddInParameter(dbCommand, "DeliveryTime", DbType.DateTime, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "DeliveryTime", DbType.DateTime, entity.DeliveryTime.Value);
			}
			if (entity.PurchaseCompany == null)
			{
			db.AddInParameter(dbCommand, "PurchaseCompany", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "PurchaseCompany", DbType.Int32, entity.PurchaseCompany.Value);
			}
			db.AddInParameter(dbCommand, "User", DbType.String, entity.User);
			db.AddInParameter(dbCommand, "AssetDescription", DbType.String, entity.AssetDescription);
			if (entity.PurchaseState == null)
			{
			db.AddInParameter(dbCommand, "PurchaseState", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "PurchaseState", DbType.Int32, entity.PurchaseState.Value);
			}
			if (entity.CreateDate == null)
			{
			db.AddInParameter(dbCommand, "CreateDate", DbType.DateTime, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "CreateDate", DbType.DateTime, entity.CreateDate.Value);
			}
			if (entity.CreateBy == null)
			{
			db.AddInParameter(dbCommand, "CreateBy", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "CreateBy", DbType.Int32, entity.CreateBy.Value);
			}
			if (entity.ModifyDate == null)
			{
			db.AddInParameter(dbCommand, "ModifyDate", DbType.DateTime, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "ModifyDate", DbType.DateTime, entity.ModifyDate.Value);
			}
			if (entity.ModifyBy == null)
			{
			db.AddInParameter(dbCommand, "ModifyBy", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "ModifyBy", DbType.Int32, entity.ModifyBy.Value);
			}

			return db.ExecuteNonQuery(dbCommand);
		  
      
        }
		
		
        /// <summary>
        /// 保存或者更新(如果传入的实体主键为空则保存,否则做更新操作)
        /// </summary>
        /// <param name="entity">传入的实体</param>
        /// <returns>影响的行数</returns>
        public override int SaveOrUpdate(tbPurchaseApplyEntity entity)
        {
			
            if (entity.ID  == 0)
            {
                if (Save(entity)>0)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return Update(entity);
            }
        }
		
		/// <summary>
        /// 保存或者更新(如果传入的实体主键为空则保存,否则做更新操作)
        /// </summary>
        /// <param name="entity">传入的实体</param>
        /// <returns>影响的行数</returns>
        public override int SaveOrUpdate()
        {
		
            if (this.ID  == 0)
            {
                if (Save()>0)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return Update();
            }
        }
		
		/// <summary>
        /// 是否存在对应主键ID的记录
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public override bool IsExist(int ID)
        {
            Database db = GetDatabase();
            StringBuilder strSql = new StringBuilder();
            			 strSql.Append("select count(1) from tb_PurchaseApply where ID=@ID ");

            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);//这里为ID是因为原有代码把主键都变成了ID.
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
		
		
		/// <summary>
        /// 删除对应的实体
        /// </summary>
        /// <param name="entity"></param>
        public override int Delete(tbPurchaseApplyEntity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete tb_PurchaseApply ");
            strSql.Append(" where ID=@ID ");
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, entity.ID );
            return db.ExecuteNonQuery(dbCommand);
        }
		
		/// <summary>
        /// 删除主键ID对应的记录
        /// </summary>
        /// <param name="id"></param>
        public override int Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete tb_PurchaseApply ");
            strSql.Append(" where ID=@ID ");
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, id);
            return db.ExecuteNonQuery(dbCommand);
        }
		
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public override int Update()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update tb_PurchaseApply set ");
			strSql.Append("[ID]=@ID,[AssetClassID]=@AssetClassID,[AssetName]=@AssetName,[AssetNum]=@AssetNum,[EstimatedPrice]=@EstimatedPrice,[ExpectTime]=@ExpectTime,[DeliveryTime]=@DeliveryTime,[PurchaseCompany]=@PurchaseCompany,[User]=@User,[AssetDescription]=@AssetDescription,[PurchaseState]=@PurchaseState,[CreateDate]=@CreateDate,[CreateBy]=@CreateBy,[ModifyDate]=@ModifyDate,[ModifyBy]=@ModifyBy ");

            strSql.Append(" where ID=@ID ");
            Database db = GetDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
			db.AddInParameter(dbCommand, "ID", DbType.Int32, this.ID );
			if (this.AssetClassID == null)
			{
			db.AddInParameter(dbCommand, "AssetClassID", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "AssetClassID", DbType.Int32, this.AssetClassID.Value);
			}
			db.AddInParameter(dbCommand, "AssetName", DbType.String, this.AssetName);
			if (this.AssetNum == null)
			{
			db.AddInParameter(dbCommand, "AssetNum", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "AssetNum", DbType.Int32, this.AssetNum.Value);
			}
			if (this.EstimatedPrice == null)
			{
			db.AddInParameter(dbCommand, "EstimatedPrice", DbType.Decimal, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "EstimatedPrice", DbType.Decimal, this.EstimatedPrice.Value);
			}
			if (this.ExpectTime == null)
			{
			db.AddInParameter(dbCommand, "ExpectTime", DbType.DateTime, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "ExpectTime", DbType.DateTime, this.ExpectTime.Value);
			}
			if (this.DeliveryTime == null)
			{
			db.AddInParameter(dbCommand, "DeliveryTime", DbType.DateTime, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "DeliveryTime", DbType.DateTime, this.DeliveryTime.Value);
			}
			if (this.PurchaseCompany == null)
			{
			db.AddInParameter(dbCommand, "PurchaseCompany", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "PurchaseCompany", DbType.Int32, this.PurchaseCompany.Value);
			}
			db.AddInParameter(dbCommand, "User", DbType.String, this.User);
			db.AddInParameter(dbCommand, "AssetDescription", DbType.String, this.AssetDescription);
			if (this.PurchaseState == null)
			{
			db.AddInParameter(dbCommand, "PurchaseState", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "PurchaseState", DbType.Int32, this.PurchaseState.Value);
			}
			if (this.CreateDate == null)
			{
			db.AddInParameter(dbCommand, "CreateDate", DbType.DateTime, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "CreateDate", DbType.DateTime, this.CreateDate.Value);
			}
			if (this.CreateBy == null)
			{
			db.AddInParameter(dbCommand, "CreateBy", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "CreateBy", DbType.Int32, this.CreateBy.Value);
			}
			if (this.ModifyDate == null)
			{
			db.AddInParameter(dbCommand, "ModifyDate", DbType.DateTime, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "ModifyDate", DbType.DateTime, this.ModifyDate.Value);
			}
			if (this.ModifyBy == null)
			{
			db.AddInParameter(dbCommand, "ModifyBy", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "ModifyBy", DbType.Int32, this.ModifyBy.Value);
			}

			
			return	db.ExecuteNonQuery(dbCommand);

		}
		



		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public override int Update(tbPurchaseApplyEntity entity)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update tb_PurchaseApply set ");
			strSql.Append("[ID]=@ID,[AssetClassID]=@AssetClassID,[AssetName]=@AssetName,[AssetNum]=@AssetNum,[EstimatedPrice]=@EstimatedPrice,[ExpectTime]=@ExpectTime,[DeliveryTime]=@DeliveryTime,[PurchaseCompany]=@PurchaseCompany,[User]=@User,[AssetDescription]=@AssetDescription,[PurchaseState]=@PurchaseState,[CreateDate]=@CreateDate,[CreateBy]=@CreateBy,[ModifyDate]=@ModifyDate,[ModifyBy]=@ModifyBy ");

            strSql.Append(" where ID=@ID ");
            Database db = GetDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
			db.AddInParameter(dbCommand, "ID", DbType.Int32, entity.ID );
			if (entity.AssetClassID == null)
			{
			db.AddInParameter(dbCommand, "AssetClassID", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "AssetClassID", DbType.Int32, entity.AssetClassID.Value);
			}
			db.AddInParameter(dbCommand, "AssetName", DbType.String, entity.AssetName);
			if (entity.AssetNum == null)
			{
			db.AddInParameter(dbCommand, "AssetNum", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "AssetNum", DbType.Int32, entity.AssetNum.Value);
			}
			if (entity.EstimatedPrice == null)
			{
			db.AddInParameter(dbCommand, "EstimatedPrice", DbType.Decimal, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "EstimatedPrice", DbType.Decimal, entity.EstimatedPrice.Value);
			}
			if (entity.ExpectTime == null)
			{
			db.AddInParameter(dbCommand, "ExpectTime", DbType.DateTime, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "ExpectTime", DbType.DateTime, entity.ExpectTime.Value);
			}
			if (entity.DeliveryTime == null)
			{
			db.AddInParameter(dbCommand, "DeliveryTime", DbType.DateTime, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "DeliveryTime", DbType.DateTime, entity.DeliveryTime.Value);
			}
			if (entity.PurchaseCompany == null)
			{
			db.AddInParameter(dbCommand, "PurchaseCompany", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "PurchaseCompany", DbType.Int32, entity.PurchaseCompany.Value);
			}
			db.AddInParameter(dbCommand, "User", DbType.String, entity.User);
			db.AddInParameter(dbCommand, "AssetDescription", DbType.String, entity.AssetDescription);
			if (entity.PurchaseState == null)
			{
			db.AddInParameter(dbCommand, "PurchaseState", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "PurchaseState", DbType.Int32, entity.PurchaseState.Value);
			}
			if (entity.CreateDate == null)
			{
			db.AddInParameter(dbCommand, "CreateDate", DbType.DateTime, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "CreateDate", DbType.DateTime, entity.CreateDate.Value);
			}
			if (entity.CreateBy == null)
			{
			db.AddInParameter(dbCommand, "CreateBy", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "CreateBy", DbType.Int32, entity.CreateBy.Value);
			}
			if (entity.ModifyDate == null)
			{
			db.AddInParameter(dbCommand, "ModifyDate", DbType.DateTime, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "ModifyDate", DbType.DateTime, entity.ModifyDate.Value);
			}
			if (entity.ModifyBy == null)
			{
			db.AddInParameter(dbCommand, "ModifyBy", DbType.Int32, System.DBNull.Value);
			}
			else
			{
			db.AddInParameter(dbCommand, "ModifyBy", DbType.Int32, entity.ModifyBy.Value);
			}

			
			return	db.ExecuteNonQuery(dbCommand);

		}
		


		
		
		
		
		/// <summary>
        /// 对象实体绑定数据
		///生成构造为list的构造器
        /// </summary>
        private tbPurchaseApplyEntity ReaderBind(IDataReader dataReader,int seq)
        {
            ITOMMS.Entity.tbPurchaseApplyEntity entity = new ITOMMS.Entity.tbPurchaseApplyEntity();
			entity.SEQNO = seq;
            object obj;
			obj = dataReader["ID"];
			 if (obj != null && obj != DBNull.Value)
			 {
			 entity.ID = (int)obj;
			 }
			 obj=dataReader["AssetClassID"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.AssetClassID= (int)obj;
			}
			 entity.AssetName= dataReader["AssetName"].ToString();
			 obj=dataReader["AssetNum"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.AssetNum= (int)obj;
			}
			 obj=dataReader["EstimatedPrice"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.EstimatedPrice= (decimal)obj;
			}
			 obj=dataReader["ExpectTime"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.ExpectTime= (DateTime)obj;
			}
			 obj=dataReader["DeliveryTime"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.DeliveryTime= (DateTime)obj;
			}
			 obj=dataReader["PurchaseCompany"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.PurchaseCompany= (int)obj;
			}
			 entity.User= dataReader["User"].ToString();
			 entity.AssetDescription= dataReader["AssetDescription"].ToString();
			 obj=dataReader["PurchaseState"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.PurchaseState= (int)obj;
			}
			 obj=dataReader["CreateDate"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.CreateDate= (DateTime)obj;
			}
			 obj=dataReader["CreateBy"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.CreateBy= (int)obj;
			}
			 obj=dataReader["ModifyDate"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.ModifyDate= (DateTime)obj;
			}
			 obj=dataReader["ModifyBy"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.ModifyBy= (int)obj;
			}

            return entity;
        }
		
		
				/// <summary>
        /// 对象实体绑定数据
		///生成构造为list的构造器
        /// </summary>
        private tbPurchaseApplyEntity ReaderBind(IDataReader dataReader)
        {
            ITOMMS.Entity.tbPurchaseApplyEntity entity = new ITOMMS.Entity.tbPurchaseApplyEntity();
			object obj;
			obj = dataReader["ID"];
			 if (obj != null && obj != DBNull.Value)
			 {
			 entity.ID = (int)obj;
			 }
			 obj=dataReader["AssetClassID"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.AssetClassID= (int)obj;
			}
			 entity.AssetName= dataReader["AssetName"].ToString();
			 obj=dataReader["AssetNum"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.AssetNum= (int)obj;
			}
			 obj=dataReader["EstimatedPrice"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.EstimatedPrice= (decimal)obj;
			}
			 obj=dataReader["ExpectTime"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.ExpectTime= (DateTime)obj;
			}
			 obj=dataReader["DeliveryTime"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.DeliveryTime= (DateTime)obj;
			}
			 obj=dataReader["PurchaseCompany"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.PurchaseCompany= (int)obj;
			}
			 entity.User= dataReader["User"].ToString();
			 entity.AssetDescription= dataReader["AssetDescription"].ToString();
			 obj=dataReader["PurchaseState"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.PurchaseState= (int)obj;
			}
			 obj=dataReader["CreateDate"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.CreateDate= (DateTime)obj;
			}
			 obj=dataReader["CreateBy"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.CreateBy= (int)obj;
			}
			 obj=dataReader["ModifyDate"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.ModifyDate= (DateTime)obj;
			}
			 obj=dataReader["ModifyBy"];
			if(obj != null && obj != DBNull.Value)
			{
			 entity.ModifyBy= (int)obj;
			}

            return entity;
        }
		
		
		
		 #endregion
		
	}
}
